#include "video Screen.h"

//Sets all the video values
bool Video::Init(HWND hWnd)
{
	m_bPlaying = false;

	CoInitialize(NULL);
	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
		IID_IGraphBuilder, (void**)&m_pGraphBuilder);

	m_pGraphBuilder->QueryInterface(IID_IMediaControl,
								(void**)&m_pMediaControl);

	m_pGraphBuilder->QueryInterface(IID_IMediaEvent,
								(void**)&m_pMediaEvent);

	m_pMediaControl->QueryInterface(IID_IVideoWindow,
								(void**)&m_pVideoWindow);

	m_pGraphBuilder->RenderFile(L"Resources/Looking For Gold Demo.AVI",NULL);

	m_pVideoWindow->put_Owner((OAHWND)hWnd);

	m_pVideoWindow->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE);

	RECT WinRect;
	GetClientRect(hWnd, &WinRect);

	m_pVideoWindow->SetWindowPosition(WinRect.left, WinRect.top,
										WinRect.right, WinRect.bottom);

	return true;	
}


//Makes sure the movie hasnt ended and continues to play it
void Video::Toggle(bool Command)
{
	long evCode;
	LONG_PTR event1,event2;

	//If its not started it starts
	// Won't check to start unless its not started
	if(!m_bPlaying && Command)
	{
		m_pMediaControl->Run();
		m_bPlaying = true;
	}

	//Checks the movies events
	m_pMediaEvent->GetEvent(&evCode, &event1, &event2, 5);

	//Ends the movie when you hit enter or it runs out
	// Wont check to stop until its started
	if((evCode == EC_COMPLETE || !Command)&&m_bPlaying)
	{
		m_pMediaControl->Stop();
		//Sets it to invisible so everything else can function
		m_pVideoWindow->put_Visible(OAFALSE);
		m_bPlaying = false;
	}
}

//Deconstructs everything
void Video::ShutDownVideo(HWND hWnd)
{
	m_pVideoWindow->put_Visible(OAFALSE);
	m_pVideoWindow->put_Owner((OAHWND)hWnd);
	m_pMediaControl->Release();
	m_pMediaEvent->Release();
	m_pGraphBuilder->Release();
}